package com._520it.crm.web.controller;

import com._520it.crm.domain.DataDictionary;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.DataDictionaryQueryObject;
import com._520it.crm.service.IDataDictionaryService;
import com._520it.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*/dataDictionary---->访问dataDictionary页面
/dataDictionary/list---->dataDictionary相关的数据
/dataDictionary/save--->保存对象*/
@Controller
@RequestMapping("/dataDictionary")
public class DataDictionaryController {
	@Autowired
	private IDataDictionaryService dataDictionaryService;
	@RequiresPermissions("dataDictionary:index")
	@PermissionName("字典列表")
	@RequestMapping("")
	public String index(){
		System.out.println("in......."+dataDictionaryService);
		return "dataDictionary";
	}
	@RequiresPermissions("dataDictionary:list")
	@PermissionName("字典数据")
	@RequestMapping("/list")
	@ResponseBody
	public PageResult list(DataDictionaryQueryObject qo){
		PageResult result = dataDictionaryService.queryPage(qo);
		return result;
	}
	@RequiresPermissions("dataDictionary:save")
	@RequestMapping("/save")
	@ResponseBody
	@PermissionName("字典新增")
	public AjaxResult save(DataDictionary dataDictionary){
		AjaxResult result = null;
		try{
			dataDictionaryService.insert(dataDictionary);
			result = new AjaxResult(true,"保存成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("保存失败,请联系管理员!");
		}
		return result;
	}
	@RequiresPermissions("dataDictionary:update")
	@PermissionName("字典更新")
	@RequestMapping("/update")
	@ResponseBody
	public AjaxResult update(DataDictionary dataDictionary){
		AjaxResult result = null;
		try{
			dataDictionaryService.updateByPrimaryKey(dataDictionary);
			result = new AjaxResult(true,"更新成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("更新失败,请联系管理员!");
		}
		return result;
	}
	@RequiresPermissions("dataDictionary:quit")
	@PermissionName("字典删除")
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult quit(Long id){
		AjaxResult result = null;
		try{
			dataDictionaryService.deleteByPrimaryKey(id);
			result = new AjaxResult(true,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			result = new AjaxResult("删除失败,请联系管理员!");
		}
		return result;
	}

	@RequestMapping("/selectAll")
	@ResponseBody
	public PageResult selectAll(){

		List<DataDictionary> all = dataDictionaryService.selectAll();
		Integer size = all.size();
		PageResult result = new PageResult(size, all);
		return result;
	}

}
